package com.boilermaker.springIoC;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class SpringIoCDemoApplication {
    public static void main(String[] args) {
        // ApplicationContext 即 Spring上下文
        // 这个上下⽂, 就是指当前的运⾏环境, 也可以看作是⼀个容器, 容器⾥存了很多内容, 这些内容是当前运⾏的环境
        ApplicationContext context = SpringApplication.run(SpringIoCDemoApplication.class, args);
        // 从上下文中获取对象
        // 1. 通过类名获取对象
        UserController userController = context.getBean(UserController.class);
        userController.say();

        // 2. 通过bean名获取对象（Spring容器会自动使用小驼峰为bean⽣成唯⼀的名称）
        //UserController userController1 = (UserController)context.getBean("userController");
        //userController1.say();

        UserController userController3 = (UserController) context.getBean("UC");
        userController3.say();

        // 3. 混合获取
        UserController userController2 = context.getBean("UC", UserController.class);
        userController2.say();

        // 使用方法注解
        NewsListener2 newsListener21 = context.getBean("newsListener1", NewsListener2.class);
        System.out.println(newsListener21);

//        NewsListener2 newsListener2 = context.getBean("newsListener2", NewsListener2.class);
//        System.out.println(newsListener2);

        NewsListener2 newsListener23 = context.getBean("n2", NewsListener2.class);
        System.out.println(newsListener23);
    }
}
